package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class SetmealServiceimpl implements SetmealService {
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    private static final String REDIS_KEY="setmeal_";

    @Override
    @Transactional
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //默认将套餐状态设为启售
        setmeal.setStatus(StatusConstant.ENABLE);
        //新增套餐，并将产生的id返回给setmeal对象
        setmealMapper.add(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && !setmealDishes.isEmpty()){
            //将setmealDishes集合中的每一个setmealDishe对象的套餐id属性赋值
            for(SetmealDish setmealDish : setmealDishes){
                setmealDish.setSetmealId(setmeal.getId());
            }
            //进行批量插入
            setmealDishMapper.add(setmealDishes);
        }
    }

    @Override
    public PageResult query(SetmealPageQueryDTO queryDTO) {
        //开启分页查询
        PageHelper.startPage(queryDTO.getPage(),queryDTO.getPageSize());
        Page<SetmealVO> result= setmealMapper.listPage(queryDTO);
        //result.getTotal()得到查询到的套餐集合的总数，result.getResult()得到查询到的套餐集合
        return new PageResult(result.getTotal(), result.getResult());
    }

    @Override
    public SetmealVO getById(Long id) {
        String key = REDIS_KEY+id;
        SetmealVO setmealVO = (SetmealVO) redisTemplate.opsForValue().get(key);
        if(setmealVO != null){
            return setmealVO;
        }
        //查询套餐信息
        setmealVO= setmealMapper.getById(id);
        //查询套菜关联的菜品集合
        List<SetmealDish> setmealDishes= setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        redisTemplate.opsForValue().set(key, setmealVO);
        return setmealVO;
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        String key = REDIS_KEY+id;
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
        redisTemplate.delete(key);
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //修改套餐信息
        setmealMapper.update(setmeal);
        //修改套餐关联菜品信息，可先删除原来数据，再插入新的数据
        List<Long> list = new ArrayList<>();
        list.add(setmeal.getId());
        //删除原来数据
        setmealDishMapper.dels(list);
        //插入新的数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && !setmealDishes.isEmpty()){
            //为每条数据设置套餐id
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
            }
            //批量插入
            setmealDishMapper.add(setmealDishes);
        }
        //获取reids以setmeal_开头的建
        Set keys= redisTemplate.keys("setmeal_*");
        //批量删除键
        redisTemplate.delete(keys);
    }

    @Override
    @Transactional
    public void del(List<Long> ids) {
        if(ids == null||ids.size()==0){
            throw new DeletionNotAllowedException(MessageConstant.NOT_SELECT);
        }
        for(Long id : ids){
            SetmealVO byId = setmealMapper.getById(id);
            Integer status = byId.getStatus();
            //判断套餐是否起售，是则不能删除
            if(status == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //批量删除套餐
        setmealMapper.dels(ids);
        //删除setmeal_dish表中关联数据
        setmealDishMapper.dels(ids);
        //获取reids以setmeal_开头的建
        Set keys= redisTemplate.keys("setmeal_*");
        //批量删除键
        redisTemplate.delete(keys);
    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        return setmealMapper.list(setmeal);
    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemById(id);
    }
}
